package com.scalyze.apps.socialanyltics.collector;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;

import com.scalyze.apps.socialanyltics.auth.api.BasicAuthenticator;
import com.scalyze.apps.socialanyltics.auth.impl.TwitterCollector;

public class TweetCollector
{
    static
    {
        BasicAuthenticator.authenticate();
    }

    public void collecTweets(String params, String hbaseMaster, String masterPort, String zkHost, String trendName)
    {
        try
        {
            com.scalyze.apps.socialanyltics.auth.api.Collector authenticator = TwitterCollector.INSTANCE;
            authenticator.authenticate(params, "https://stream.twitter.com/1/statuses/filter.json");
            if (authenticator.getResponseCode() == HttpURLConnection.HTTP_OK)
            {
                BufferedReader br = new BufferedReader(new InputStreamReader(authenticator.getStream(), "UTF-8"));

                String line = null;
                int counter = 0;
                while ((line = br.readLine()) != null)
                {
                    System.out.println(line);
                    counter++;
                    authenticator.collectTweets(line,hbaseMaster,masterPort,zkHost,trendName);
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {

        }

    }

}
